const db = require('../utils/settings');
const md5 = require('../utils/md5');

function Login() {

}

//登录操作
Login.prototype.login = function (req, res, next) {
    var username = req.body.username;
    var password = md5.md5(req.body.password+md5.MD5_SUFFIX);
    if(username && password){
        db.query('SELECT * FROM admin_table WHERE username="'+username+'"',function (err,userData) {
            if(err){
                console.error(err);
                res.status(500).send({code:500,data:[],msg:'database error'});
            }else if(userData.length == 0){
                res.status(400).send({code:400,data:[],msg:'parameters error'});
            }else{
                if(userData[0].password != password){
                    res.status(400).send({code:400,data:[],msg:'username or password error'});
                }else{
                    req.session['user_id'] = userData[0].ID;//注意这里是在req上面
                    res.status(200).send({code:200,data:[],msg:'success'});
                }
            }
        })
    }else{
        res.status(400).send({code:400,data:[],msg:'parameters error'});
    }
}

module.exports = new Login();